package com.xpxrule.strategy;

import com.xpxrule.constant.Operator;
import com.xpxrule.strategy.operator.*;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 操作符策略上下文
 * @version 1.0
 * @Author xiangpengxiang
 * @Date 2025/1/7 15:56
 * @注释
 */
public class OperatorStrategyContext {
    private static ConcurrentHashMap<Operator, OperatorStrategy> operatorStrategyMap;

    /**
     * 初始化操作符策略上下文
     */
    static {
        operatorStrategyMap = new ConcurrentHashMap<>();
        operatorStrategyMap.put(Operator.NULL, new NullOperatorStrategy());
        operatorStrategyMap.put(Operator.NOT_NULL, new NotNullOperatorStrategy());
        operatorStrategyMap.put(Operator.EQUAL, new EqualOperatorStrategy());
        operatorStrategyMap.put(Operator.NOT_EQUAL, new NotEqualOperatorStrategy());
        operatorStrategyMap.put(Operator.GREATER_THAN, new GreaterThanOperatorStrategy());
        operatorStrategyMap.put(Operator.GREATER_THAN_OR_EQUAL, new GreaterThanOrEqualOperatorStrategy());
        operatorStrategyMap.put(Operator.LESS_THAN, new LessThanOperatorStrategy());
        operatorStrategyMap.put(Operator.LESS_THAN_OR_EQUAL, new LessThanOrEqualOperatorStrategy());
        operatorStrategyMap.put(Operator.BETWEEN, new BetweenOperatorStrategy());
        operatorStrategyMap.put(Operator.NOT_BETWEEN, new NotBetweenOperatorStrategy());
        operatorStrategyMap.put(Operator.CONTAINS, new ContainsOperatorStrategy());
        operatorStrategyMap.put(Operator.NOT_CONTAINS, new NotContainsOperatorStrategy());
        operatorStrategyMap.put(Operator.IN, new InOperatorStrategy());
        operatorStrategyMap.put(Operator.NOT_IN, new NotInOperatorStrategy());
        operatorStrategyMap.put(Operator.REGEX, new RegexOperatorStrategy());
    }

    /**
     * 获取操作符策略
     * @param operator
     * @return
     */
    public static OperatorStrategy getOperatorStrategy(Operator operator) {
        return operatorStrategyMap.get(operator);
    }

    public static OperatorStrategy getOperatorStrategy(String operatorCode) {
        Operator operator = Operator.getByCode(operatorCode);
        return getOperatorStrategy(operator);
    }
}
